package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceIml implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新建菜品
     *
     * @param dto
     */
    @Transactional
    @Override
    public void addDish(DishDTO dto) {

        //创建菜品对象补充数据
        Dish dish = new Dish();
        //拷贝数据
        BeanUtils.copyProperties(dto, dish);

        Long id = dishMapper.addDish(dish);

        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors == null || flavors.isEmpty()) {
            return;
        }
        //给集合里的id属性赋值
        flavors.forEach(flavor -> flavor.setDishId(id));

        dishMapper.addDishFlavor(flavors);
    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<DishVO> dishList = dishMapper.page(dto);

        return new PageResult(dishList.getTotal(), dishList.getResult());
    }

    /**
     * 修改菜品回显
     *
     * @param dishId
     * @return
     */
    @Override
    public DishVO getDish(Long dishId) {
        //查询dish属性
        DishVO dish = dishMapper.getDish(dishId);
        //查询风味集合
        List<DishFlavor> flavor = dishMapper.getDishFlavor(dishId);
        dish.setFlavors(flavor);
        return dish;

    }

    /**
     * 修改菜品
     *
     * @param dto
     */
    @Transactional
    @Override
    public void updateDish(DishDTO dto) {
        //为了简化代码，可以将原来数据库数据删除，再重新保存

        List<Long> list = new ArrayList<>();
        list.add(dto.getId());

        List flavorList = dto.getFlavors();
        if (flavorList != null && !flavorList.isEmpty()) {
            dishMapper.deleteFlavors(list);
        }

        //创建菜品对象补充数据
        Dish dish = new Dish();
        //拷贝数据
        BeanUtils.copyProperties(dto, dish);

        dishMapper.updateDish(dish);

        Long id = dish.getId();

        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors == null || flavors.isEmpty()) {
            return;
        }
        //给集合里的id属性赋值
        flavors.forEach(flavor -> flavor.setDishId(id));

        dishMapper.addDishFlavor(flavors);
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Transactional
    @Override
    public void delecteDish(List<Long> ids) {
        for (Long id : ids) {
            Integer status = dishMapper.getDish(id).getStatus();
            if (status == 1) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }

            SetmealDish setMealDish = dishMapper.getSetMealDish(id);
            if (setMealDish != null) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }

        }
        dishMapper.deleteDish(ids);
        dishMapper.deleteFlavors(ids);
    }

    /**
     * 菜品起售停售
     *
     * @param status
     * @param id
     */
    @Override
    public void ControlDishStatus(Integer status, Long id) {
        //关联套餐，如果菜品是停售状态，则此菜品加入的套餐都将设置为停售
        if(status==0){
            List<Long> list = setmealMapper.getSetmealId(id);
            for (Long setmealId : list) {
                setmealMapper.ControlDishStatus(status,setmealId);
            }
        }


        Dish dish = new Dish();
        DishVO dishvo = dishMapper.getDish(id);
        BeanUtils.copyProperties(dishvo, dish);
        dish.setStatus(status);

        dishMapper.updateDish(dish);

    }

    /**
     * 分类查询，用于新增套餐
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<DishVO> querycategory(Long categoryId) {

        List<DishVO> result = dishMapper.querycategory(categoryId);

        return result;
    }


    /**
     * 用户端菜品查询
     * @param categoryId
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Long categoryId) {

        List<DishVO> dishVOList = dishMapper.querycategory(categoryId);

        for (DishVO dishVO : dishVOList) {

            if(dishVO.getStatus()==0){
                continue;
            }
            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishMapper.getDishFlavor(dishVO.getId());

            dishVO.setFlavors(flavors);
        }
        return dishVOList;

    }


}
